package com.wulang.extension.config;


import com.wulang.extension.constants.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class DLXConfig {
    @Bean("dlqQueue")
    public Queue dlqQueue() {
        return QueueBuilder.durable(Constants.DLQ_QUEUE).build();
    }

    @Bean("dlxExchange")
    public Exchange dlxExchange() {
        return ExchangeBuilder.directExchange(Constants.DLX_EXCHANGE).build();
    }

    @Bean("dbBding")
    public Binding dbBding(@Qualifier("dlqQueue") Queue queue, @Qualifier("dlxExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("dlx").noargs();
    }

    //将死信加交换机和正常队列绑定
    @Bean("normalQueue")
    public Queue normalQueue() {
        return QueueBuilder.durable(Constants.NORMAL_QUEUE)
                .deadLetterExchange(Constants.DLX_EXCHANGE)
                .deadLetterRoutingKey("dlx")
                .maxLength(10)
                .ttl(20000).build();
    }

    @Bean("normalExchange")
    public Exchange normalExchange() {
        return ExchangeBuilder.directExchange(Constants.NORMAL_EXCHANGE).build();
    }
    @Bean("normalBinding")
    public Binding normalBinding(@Qualifier("normalQueue") Queue queue, @Qualifier("normalExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("normal").noargs();
    }

}
